package com.zzgele.qmzb.api.utils;

import com.alibaba.fastjson.JSONObject;

import javax.net.ssl.*;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

/**
 * Created by Martin on 2017-08-08.
 */
public class IapUtil {

    /**
     * 获取苹果内支付商品金额
     * @param goodsType 商品类型【定义在APPLE】
     * @return
     */
    public static BigDecimal getIpaMoney(String goodsType) {
        switch (goodsType) {
            case "com.42":
                return new BigDecimal(6);
            case "com.210":
                return new BigDecimal(30);
            case "com.686":
                return new BigDecimal(98);
            case "com.2086":
                return new BigDecimal(298);
            case "com.4116":
                return new BigDecimal(588);
            case "com.11186":
                return new BigDecimal(1598);
            default:
                return new BigDecimal(0);
        }
    }

    /**
     * 发送https请求
     * @param url
     * @param code
     * @return
     */
    public static String sendHttpsCoon(String url, String code){
        if(url.isEmpty()){
            return null;
        }
        HttpsURLConnection conn = null;
        try {
            //设置SSLContext
            SSLContext ssl = SSLContext.getInstance("SSL");
            ssl.init(null, new TrustManager[]{myX509TrustManager}, null);

            //打开连接
            conn = (HttpsURLConnection) new URL(url).openConnection();
            //设置套接工厂
            conn.setSSLSocketFactory(ssl.getSocketFactory());
            //加入数据
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-type","application/json");

            conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
            //conn.setRequestProperty("Proxy-Connection", "Keep-Alive");
            conn.setDoInput(true);
            // 连接超时
            conn.setConnectTimeout(25000);
            // 读取超时 --服务器响应比较慢，增大时间
            conn.setReadTimeout(25000);
            conn.connect();

            JSONObject obj = new JSONObject();
            obj.put("receipt-data", code);

            BufferedOutputStream buffOutStr = new BufferedOutputStream(conn.getOutputStream());
            buffOutStr.write(obj.toString().getBytes());
            buffOutStr.flush();
            buffOutStr.close();

            //获取输入流
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

            String line = null;
            StringBuffer sb = new StringBuffer();
            while((line = reader.readLine())!= null){
                sb.append(line);
            }
            reader.close();
            return sb.toString();

        } catch (Exception e) {
            return null;
        } finally {
            if (conn != null) {
                conn.disconnect();// 关闭连接
            }
        }
    }

    /**
     * 重写X509TrustManager
     */
    private static TrustManager myX509TrustManager = new X509TrustManager() {
        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }
        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }
    };

    private static class TrustAnyHostnameVerifier implements HostnameVerifier {
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    }
}
